package com.bro.person.admin.service.impl;

import com.bro.person.base.model.paging.PagingResult;
import com.bro.person.base.service.impl.BroBaseServiceImpl;
import com.bro.person.admin.dao.mapper.AdminRoleMapper;
import com.bro.person.admin.model.AdminRole;
import com.bro.person.admin.model.AdminRoleCriteria;
import com.bro.person.admin.service.IAdminRoleService;

import com.bro.person.base.constants.ConstConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * AdminRole的 DB操作service
 *
 * @author jdd
 * @version 1.0
 * @date 2018-10-11 09:53:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdminRoleServiceImpl extends BroBaseServiceImpl<AdminRole, AdminRoleCriteria, Integer> implements IAdminRoleService {

	private static Logger logger = LoggerFactory.getLogger(AdminRoleServiceImpl.class);

	@Resource
	@SuppressWarnings("SpringJavaAutowiringInspection")
	private AdminRoleMapper adminRoleMapper;

	@Override
	public List<AdminRole> queryAll(String orderByClause) {
		logger.debug("[AdminRoleServiceImpl][queryAll]entering, orderByClause:{}", orderByClause);
		AdminRoleCriteria criteria = new AdminRoleCriteria();
		criteria.createCriteria();
		criteria.setOrderByClause(orderByClause);
		return queryByCriteria(criteria);
	}

	@Override
	public List<AdminRole> fallloadByCriteria(int pageSize, AdminRoleCriteria criteria) {
		logger.debug("[AdminRoleServiceImpl][fallloadByCriteria]entering, pageSize:{}", pageSize);
		return null;
	}

	@Override
	public PagingResult<AdminRole> pagingByCriteria(int pageNo, int pageSize, AdminRoleCriteria criteria, AdminRole adminRole) {
		logger.debug("[AdminRoleServiceImpl][pagingByCriteria] entering, pageNo:{},pageSize:{}", pageNo, pageSize);
		// 确保pageNo合法
		pageNo = pageNo <= 0 ? 1 : pageNo;
		pageSize = pageSize <= 0 ? ConstConfig.PAGE_SIZE_DEFAULT : pageSize;
		// 确保pageSize合法
		int offset = (pageNo - 1) * pageSize;

		// 构造查询条件
		if (criteria == null) {
			criteria = new AdminRoleCriteria();
		}

		criteria.setLimitOffset(offset);
		criteria.setLimitRows(pageSize);
		logger.info("[AdminRoleServiceImpl][pagingByCriteria] offset:{}, pageNo: {},pageSize:{}", offset, pageNo, pageSize);
		int count = super.countByCriteria(criteria);
		List<AdminRole> dataList = adminRoleMapper.selectByExample(criteria);
		// 返回分页数据
		logger.info("[AdminRoleServiceImpl][pagingByCriteria] result, pageNo: {},pageSize:{},count:{},dataListSize:{}",
				pageNo, pageSize, count, dataList != null ? dataList.size() : 0);
		return new PagingResult<>(pageNo, pageSize, count, dataList);
	}

	@Override
	public AdminRoleMapper getBaseMapper() {
			return adminRoleMapper;
	}
}